﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VocabManager.Model;
using VocabManager.DataLayer;
using VocabManager.Command;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;

namespace VocabManager.ViewModel
{
    public class ItemViewModel : ViewModelBase
    {
        public bool IsNew
        {
            get;
            set;
        }
        public Item Item
        {
            get;
            private set;
        }

        DeckViewModel _deckViewModel;
        public DeckViewModel DeckViewModel
        {
            get
            {
                return _deckViewModel;
            }
        }
        public ItemViewModel(Item item, DeckViewModel deckViewModel)
        {
            Item = item;
            _deckViewModel = deckViewModel;
            Item.DeckId = deckViewModel.Id;
           
        }
        public int DeckId
        {
            get
            {
                return Item.DeckId;
            }
        }
        protected override void OnPropertyChanged(string propertyName)
        {
            base.OnPropertyChanged(propertyName);

        }
        public int Index
        {
            get 
            {
                return _deckViewModel.AllItems.IndexOf(this) + 1;
            }
        }
        public string FrontText
        {
            get
            {
                return Item.Front.Text ;
            }
            set
            {
                if (Item.Front.Text == value)
                    return;
                Item.Front.Text = value;
                OnPropertyChanged("FrontText");
            }
        }
      
        public string BackText
        {
            get
            {
                return Item.Back.Text;
            }
            set
            {
                if (Item.Back.Text == value)
                    return;
                Item.Back.Text = value;
                OnPropertyChanged("BackText");
            }
        }
        public string BackSound
        {
            get
            {
                return Item.Back.Sound;
            }
            set
            {
                if (Item.Back.Sound == value)
                    return;
                Item.Back.Sound = value;
                OnPropertyChanged("BackSound");
            }
        }
        bool _isInEditMode;
        public bool IsInEditMode
        {
            get
            {
                return _isInEditMode;
            }
            set
            {
                if (_isInEditMode == value)
                    return;
                _isInEditMode = value;
                OnPropertyChanged("IsInEditMode");
            }
        }
        bool _isFrontSide = true;
        public bool IsFrontSide
        {
            get
            {
                return _isFrontSide;
            }
            set
            {
                if (_isFrontSide == value)
                    return;
                _isFrontSide = value;
                OnPropertyChanged( "IsFrontSide" );
            }
        }
        #region Edit
        RelayCommand _editCommand;
        public ICommand EditCommand
        {
            get
            {
                if (_editCommand == null)
                {
                    _editCommand = new RelayCommand(
                        param => this.Edit(),
                        param => this.CanEdit()
                        );
                }
                return _editCommand;
            }
        }
        private bool CanEdit()
        {
            return true;
        }

        private void Edit()
        {
            IsInEditMode = true;
            BeginEdit();
        }

        void BeginEdit()
        {
            _tempPropertyValues = new Dictionary<string, object>();
            _tempPropertyValues.Add("FrontText", this.FrontText);
            _tempPropertyValues.Add("BackText", this.BackText);

        }
        #endregion
      

        #region Cancel
        RelayCommand _cancelEditCommand;
        public ICommand CancelEditCommand
        {
            get
            {
                if (_cancelEditCommand == null)
                {
                    _cancelEditCommand = new RelayCommand(
                        param => this.CancelEdit(),
                        param => this.CanCancelEdit()
                        );
                }
                return _cancelEditCommand;
            }
        }

      

        private bool CanCancelEdit()
        {
            return IsInEditMode;
        }

        Dictionary<string, object> _tempPropertyValues;

        private void CancelEdit()
        {
            if (IsNew)
                _deckViewModel.RemoveCurrentCommand.Execute(null);
            else
            {

                this.FrontText = _tempPropertyValues["FrontText"] as string;
                this.BackText = _tempPropertyValues["BackText"] as string;
            }
            this.IsInEditMode = false;
        }

#endregion

        #region Save
     

        RelayCommand _saveCommand;
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new RelayCommand(
                        param => this.Save(),
                        param => this.CanSave()
                        );
                }
                return _saveCommand;
            }
        }


        private void Save()
        {
            _deckViewModel.SaveItem(this);
          this.IsNew =  this.IsInEditMode = false;
           
        }

  

        private bool CanSave()
        {
            return IsInEditMode;
        }

        #endregion

        #region Change Side
        internal void ChangeSide()
        {
            IsFrontSide = !IsFrontSide;
        }

        internal bool CanChangeSide()
        {
            return !IsInEditMode;
        }

        RelayCommand _changeSideCommand;
        public ICommand ChangeSideCommand
        {
            get
            {
                if (_changeSideCommand == null)
                {
                    _changeSideCommand = new RelayCommand(
                        param => this.ChangeSide(),
                        param => this.CanChangeSide()
                        );
                }
                return _changeSideCommand;
            }
        }
#endregion


       
    }
}
